<?php
namespace Modules\Buy\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Modules\Buy\Entities\Arr;
use Modules\Buy\Entities\Catego;
use Modules\Buy\Entities\Good;
use Modules\Buy\Entities\Huowu;
use Modules\Buy\Http\Requests\HuowuRequest;
class HuowuController extends Controller
{
    //显示列表
    public function index(Good $good)
    {

//        dd($good->toArray());
//        dd($id);
//        dd($huowu->toArray());
        $data = Huowu::where('good_id',$good->id)->paginate(10);
        return view('buy::huowu.index', compact('data','huowu','good'));
    }

    //创建视图
    public function create(Huowu $huowu,Good $good)
    {
//        dd(123);
//        可以获得商品的那一条数据
//        dd($good->toArray());explode是把字符串拆分成数组
        $attrs = explode(',',($good->toArray()['can_id']));
//        dd($attrs);
        $shuxing = Arr::whereIn('pid',$attrs)->pluck('id')->toArray();
//        dd($shuxing);
        //所有属性多级
        $att = \houdunwang\arr\Arr::channelLevel(Arr::whereIn('id',array_merge ($attrs,$shuxing))->get()->toArray(),0, $html = "&nbsp;",'id','pid');
//        dd($att);
        return view('buy::huowu.create',compact('huowu','good','att'));

    }

    //保存数据
    public function store(HuowuRequest $request,Huowu $huowu,Good $good)
    {
//        dd(123);
//        注意添加的时候的商品id,也是要往里边添加的
        $data = $request->all();
//        dd($data);
//        $data['good_id'] = $good->id;
//        使用模型关联往进加数据
        $data['attr'] = implode(',',$data['attr']);
//        dd($post);
        $good->huowulist()->create($data);
        return redirect('/buy/huowu/'.$good->id)->with('success', '保存成功');
    }

    //显示记录
    public function show(Huowu $field,Good $good)
    {

        return view('buy::huowu.show', compact('field','good'));
    }

    //编辑视图
    public function edit(Good $good,Huowu $huowu)
    {

//        dd($huo->toArray());

//        dd($catego->toArray());
//        dd($good->toArray());
//        $res= Huowu::get();
//        dd($res->toArray());
        $attrs = explode(',',($good->toArray()['can_id']));
//        dd($attrs);
        $shuxing = Arr::whereIn('pid',$attrs)->pluck('id')->toArray();
//        dd($shuxing);
////     所有属性多级
        $att = \houdunwang\arr\Arr::channelLevel(Arr::whereIn('id',array_merge ($attrs,$shuxing))->get()->toArray(),0, $html = "&nbsp;",'id','pid');
//        dd($att);

//        $hw= Huowu::where('id',$good->id)->find($good->id);
//        dd($hw->toArray());
//        dd($huowu->toArray());
        return view('buy::huowu.edit', compact('huowu','att','good'));
    }

    //更新数据
    public function update(HuowuRequest $request,Good $good, Huowu $huowu)
    {
//        dd(123);
//        dd($request->all());
        $post = $request->all();
//        dd($huowu->toArray());
//        dd($post);
        $post['attr']= implode(',',$post['attr']);
//        dd($post['attr']);
        $huowu->update($post);
        return redirect('/buy/huowu/'.$huowu->id)->with('success','更新成功');
    }

    //删除模型
//    public function destroy(Huowu $huowu,Good $good)
//    {
////        dd(123);
//        $huowu->delete();
//        return redirect('buy/huowu/'.$good->id)->with('success','删除成功');
//    }

    public function del(Huowu $huowu,Good $good)
    {
//        dd($huowu->toArray());
//        $hw= Huowu::where('good_id',$good->id)->get();
//        dd(123);
        $huowu->delete();
        return redirect('buy/huowu/'.$huowu->id)->with('success','删除成功');
    }


}
